package com.lin.services.impl;

import com.lin.dao.UserDao;
import com.lin.pojo.User;
import com.lin.services.BasicService;
import com.lin.utils.MD5Util;
import com.lin.utils.ShiroUtil;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Class:BasicServiceImpl
 * Package:com.lin.services.impl
 *
 * @author:hellcat Date:2020/2/16
 * Description:
 * 🐾
 */
@Service
public class BasicServiceImpl implements BasicService {

    @Autowired
    private UserDao userDao;

    @Override
    public Map<String, Object> updateBasic(String object, String value) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", false);
        map.put("msg", "修改失败");
        User user = ShiroUtil.getUser();
        if ("".equals(value.trim())) {
            map.put("msg", "输入格式不规范");
            return map;
        }
        if ("basicPhone".equals(object)) {
            Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");
            Matcher m = p.matcher(value);
            if (!m.matches()) {
                map.put("msg", "手机号不规范");
                return map;
            }
            user.setPhone(value.trim());
        } else if ("basicName".equals(object)) {
            user.setName(value.trim());
        }
        int i = userDao.updateUser(user);
        if (i == 1) {
            map.put("code", true);
            if ("basicPhone".equals(object)) {
                map.put("value", user.getPhone());
            } else {
                map.put("value", user.getName());
            }
            try {
                ShiroUtil.clearUserAuthorizationInfo(user.getAccountNumber());
            } catch (Exception e) {
                map.clear();
                map.put("code", false);
                map.put("msg", "修改失败，未知异常");
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        }
        return map;
    }

    @Override
    public Map<String, Object> updatePwd(String oldPwd, String newPwd) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", false);
        map.put("msg", "密码修改失败");
        if (oldPwd == null || "".equals(oldPwd.trim())
                || newPwd == null || "".equals(newPwd.trim())) {
            map.put("msg", "输入不规范");
            return map;
        }
        //        在这新密码规范性判断 ^(?=.*\d)(?=.*[A-Za-z])[\x21-\x7e]{4,16}$  4-16位
//        可以包含字母数字和常用特殊字符，但不能是纯字母纯数字
        Pattern p = Pattern.compile("^(?=.*\\d)(?=.*[A-Za-z])[\\x21-\\x7e]{4,16}$");
        Matcher m = p.matcher(newPwd);
        if (!m.matches()) {
            map.put("msg", "密码输入不规范");
            return map;
        }
        User user = ShiroUtil.getUser();
        String oldEncrypted = MD5Util.encrypted(ShiroUtil.getUser().getAccountNumber(), oldPwd);
        String newEncrypted = MD5Util.encrypted(ShiroUtil.getUser().getAccountNumber(), newPwd);
        if (user == null || !oldEncrypted.equals(user.getPwd())) {
            map.put("msg", "旧密码错误");
            return map;
        }
        user.setPwd(newEncrypted);
        int i = userDao.updateUser(user);
        if (i == 1) {
            map.put("code", true);
            try {
                ShiroUtil.clearAllCachedInfo(ShiroUtil.getUser().getAccountNumber());
                SecurityUtils.getSubject().logout();
            } catch (Exception e) {
                System.out.println("缓存清理失败");
            }
        }
        return map;
    }

}
